package com.yunshang.shoppingmall.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.LayUIUtil;
import com.yunshang.shoppingmall.common.MD5Util;
import com.yunshang.shoppingmall.entity.Admin;
import com.yunshang.shoppingmall.entity.Role;
import com.yunshang.shoppingmall.entity.UserRolePrivileges;
import com.yunshang.shoppingmall.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 后台管理员Admin
 */
@Controller
public class AdminController {

    @Autowired
    AdminService adminService;
    @Autowired
    PrivilegeService privilegeService;
    @Autowired
    RoleService roleService;
    @Autowired
    UserRolePrivilegesService userRolePrivilegesService;
    @Autowired
    PrivilegeURLService privilegeURLService;


    //管理列表页面
    @RequestMapping("/adminList")
    public String getAdminList() {
        return "admin/adminList";
    }


    //    //管理员列表的数据
    @ResponseBody
    @RequestMapping(value = "/adminListData")
    public LayUIUtil getAdminListData(HttpServletRequest request) {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        int pages = 0;
        int limits = 10;
        if (page != null && page != "") {
            pages = Integer.parseInt(page);
        }
        if (limit != null && limit != "") {
            limits = Integer.parseInt(limit);
        }
        PageHelper.startPage(pages, limits);
        List<Admin> adminList = adminService.selectAdminList();
        int count = 0;
        count = adminList.size();
        return LayUIUtil.data(count, adminList);

    }

    //根据id编辑管理员
    @RequestMapping("/editorAdminById")
    public ModelAndView editorAdmin(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("admin/editAdmin");
        String id = request.getParameter("adminId");
        int adminId = 0;
        if (id != null && id != "") {
            adminId = Integer.parseInt(id);
        }
        Admin admin = adminService.selectByPrimaryKey(adminId);
        modelAndView.addObject("admin", admin);
        return modelAndView;
    }


    //保存编辑管理员的数据
    @ResponseBody
    @RequestMapping(value = "/savaEditAdminData", method = RequestMethod.POST)
    public Map<String, Object> savaEditAdminData(HttpServletRequest request, @RequestParam Map<String, String> map) {
        Map<String, Object> map1 = new HashMap<>();
        int adminId = 0;
        String adminName = null;
        String realName = null;
        String id = null;
        Admin admin = new Admin();
        if (map != null) {
            id = map.get("adminId");
            adminId = Integer.parseInt(id);
            adminName = map.get("adminName");
            realName = map.get("realName");
        }
        //保存
        admin.setAdminName(adminName);
        admin.setRealName(realName);
        admin.setAdminId(adminId);
        int row = adminService.updateByPrimaryKeySelective(admin);
        if (row > 0) {
            map1.put("msg", "1");
        } else {
            map1.put("msg", "0");
        }
        return map1;
    }


    //删除管理员
    @ResponseBody
    @RequestMapping("/delAdmin")
    public Map<String, Object> delAdmin(HttpServletRequest request, @RequestParam Map<String, String> map) {
        Map<String, Object> map1 = new HashMap<>();
        int adminId = 0;
        if (map != null) {
            String id = map.get("adminId");
            adminId = Integer.parseInt(id);
            int row = adminService.deleteByPrimaryKey(adminId);
            if (row > 0) {
                map1.put("msg", "1");
            } else {
                map1.put("msg", "0");
            }
        }
        return map1;
    }

    //添加管理员
    @ResponseBody
    @RequestMapping(value = "/addAdmin", method = RequestMethod.POST)
    public ModelAndView addAdmin(HttpServletRequest request, @RequestParam Map<String, String> map) {
        ModelAndView modelAndView = new ModelAndView("admin/addAdmin");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "saveAddAdmin", method = RequestMethod.POST)
    public Map<String, Object> saveAddAdmin(HttpServletRequest request, @RequestParam Map<String, String> map) {
        Map<String, Object> map1 = new HashMap<>();
        Admin admin = new Admin();
        admin.setRealName(map.get("realname"));
        String encryPwd = null;
        try {
            encryPwd = MD5Util.getEncryptedPwd(map.get("password"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        admin.setPwd(encryPwd);

        admin.setAdminName(map.get("adminName"));
        int row = adminService.insertSelective(admin);
        if (row > 0) {
            map1.put("msg", "1");
        } else {
            map1.put("msg", "0");
        }
        return map1;
    }


    //查询管理员名称
    @ResponseBody
    @RequestMapping("/queryAdminName")
    public String queryAdminName(@RequestParam("adminName") String adminName) {
        List<Admin> adminList = null;
        if (adminName != null && adminName != "") {
            adminList = adminService.selectAdminByAdminName(adminName);
            if (adminList != null && adminList.size() != 0) {
                return "1";
            } else {
                return "0";
            }
        }
        return "0";
    }


    //增加管理员弹窗页面
    @RequestMapping("/addAdmin")
    public String addAdmin() {

        return "admin/addAdmin";
    }

    //增加管理员数据
    @ResponseBody
    @RequestMapping(value = "/addAdminData")
    public Map<String, Object> addAdminData(@RequestParam("username") String username,
                                            @RequestParam("password") String password,
                                            @RequestParam("realname") String realname,
                                            @RequestParam("limited") String limited) {
        Logger logger = LoggerFactory.getLogger(AdminController.class);
        System.out.println("---->" + realname);
        Map<String, Object> map = new HashMap<>();
        if (username != "" && password != "" && realname != "" && limited != "") {
            Admin admin = new Admin();
            admin.setAdminName(username);
            admin.setPwd(password);
            admin.setRealName(realname);
            admin.setLimited(limited);
            logger.info(admin.toString());
            int rows = adminService.insert(admin);

            if (rows > 0) {
                map.put("result", rows);
                return map;
            } else {
                map.put("result", "添加失败！");
                return map;
            }

        } else {
            map.put("result", "添加的值不能为空！");
            return map;
        }

    }


    //根据adminId查询admin
    @RequestMapping("/selectAdminByAdminId")
    public ModelAndView getAdmin(String adminId) {

        System.out.println("---->sel=" + adminId);
        ModelAndView modelAndView = new ModelAndView("admin/editAdmin");
        //根据adminId查询

        int adId = Integer.valueOf(adminId);
        Admin admin = adminService.selectByPrimaryKey(adId);
        System.out.println("--admin--->" + admin);
        modelAndView.addObject(admin);
        return modelAndView;


    }

    //进入管理员编辑页面
    @RequestMapping("/editAdmin")
    public ModelAndView updateAdmin(String adminId) {
        System.out.println("adminId=" + adminId);
        int adminIds = Integer.valueOf(adminId);
        ModelAndView modelAndView = new ModelAndView("admin/editAdmin");
        Admin admin = adminService.selectByPrimaryKey(adminIds);
        modelAndView.addObject("admin", admin);
        return modelAndView;
    }

    //保存编辑后的管理员信息
    @ResponseBody
    @RequestMapping("/saveEditAdmin")
    public String saveEditAdmin(@RequestParam("adminId") String adminId,
                                @RequestParam("username") String username,
                                @RequestParam("password") String password,
                                @RequestParam("realname") String realname,
                                @RequestParam("limited") String limited) {
//        Map<String ,Object> map=new HashMap<>();
        if (adminId != "" && username != "" && password != "" && realname != "" && limited != "") {
            int adminIds = Integer.parseInt(adminId);
            Admin admin = new Admin();
            admin.setAdminId(adminIds);
            admin.setAdminName(username);
            admin.setPwd(password);
            admin.setRealName(realname);
            admin.setLimited(limited);
            // int rows=adminService.insert(admin);
            int rows = adminService.updateByPrimaryKeySelective(admin);

            if (rows > 0) {
//                map.put("result",rows);
                return "success";
            } else {
//                map.put("result","添加失败！");
                return "faile";
            }

        } else {
//            map.put("result","添加的值不能为空！");
            return "faile";
        }


    }

    /*管理员权限*/
    @ResponseBody
    @RequestMapping("/adminRolePrivilege")
    public ModelAndView adminRolePrivilege() {
        ModelAndView modelAndView = new ModelAndView("admin/admin_role_privilege");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/getAdminRolePrivilegeData")
    public LayUIUtil getAdminRolePrivilegeData(HttpServletRequest request) {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        int pages = 0;
        int limits = 10;
        if (page != null && page != "") {
            pages = Integer.parseInt(page);
        }
        if (limit != null && limit != "") {
            limits = Integer.parseInt(limit);
        }
        PageHelper.startPage(pages, limits);
        List<UserRolePrivileges> userRolePrivilegesList = userRolePrivilegesService.selectUserRolePrivilegeList();
        PageInfo<UserRolePrivileges> pageInfo = new PageInfo<>(userRolePrivilegesList);
        return LayUIUtil.data((int) pageInfo.getTotal(), pageInfo.getList());

    }

    /*查看管理员*/
    @ResponseBody
    @RequestMapping("/editAdminRolePrivilege")
    public ModelAndView editAdminRolePrivilege(@RequestParam("adminId") String adminId) {
        ModelAndView modelAndView = new ModelAndView("admin/editoradminroleprivilege");
        Map<String, Object> map = new HashMap<>();
        int adminIds = 0;
        if (adminId != null && adminId != "") {
            adminIds = Integer.parseInt(adminId);
        }
        List<UserRolePrivileges> userRolePrivilegesList = userRolePrivilegesService.selectUserRolePrivilegesByAdminId(adminIds);
        UserRolePrivileges userRolePrivileges=new UserRolePrivileges();
       List<Role> roleList= roleService.selectList();
        map.put("userRolePrivilegesList", userRolePrivilegesList);
        map.put("roleList",roleList);
        modelAndView.addObject("map", map);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/queryAdminRolePrivilege")
    public Map<String,Object> queryAdminRolePrivilege(@RequestParam("adminId") String adminId) {
        Map<String, Object> map = new HashMap<>();
        int adminIds = 0;
        if (adminId != null && adminId != "") {
            adminIds = Integer.parseInt(adminId);
        }
        List<UserRolePrivileges> userRolePrivilegesList = userRolePrivilegesService.selectUserRolePrivilegesByAdminId(adminIds);
        UserRolePrivileges userRolePrivileges=new UserRolePrivileges();
        for (int i = 0; i <userRolePrivilegesList.size() ; i++) {
            userRolePrivileges=userRolePrivilegesList.get(i);
        }
        map.put("userRolePrivileges",userRolePrivileges);
        return map;
    }

}
